package com.fjwt.gz.service.activity.assign.acte023;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.db.dto.FansActivityDTO;
import com.fjwt.gz.db.entity.ScoreRecordTotalEntity;
import com.fjwt.gz.db.vo.ActivityAwardVO;
import com.fjwt.gz.service.activity.BaseActivityDTO;
import com.fjwt.gz.service.activity.BaseActivityService;
import com.fjwt.gz.service.impl.ActivityAwardService;
import com.fjwt.gz.service.impl.ScoreRecordTotalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 一次性派发积分活动------
 */
@Slf4j
@Service
public class ACTE023Service extends BaseActivityService<Object> {

    private static String logPrefix = "【一次性派发积分活动】";
    @Autowired
    private ActivityAwardService activityAwardService;
    @Autowired
    private ScoreRecordTotalService scoreRecordTotalService;

    /**
     * 新建活动
     */
    @Override
    public void add(BaseActivityDTO activityDTO) {
        log.info("新增ACTE023的活动");
        super.add(activityDTO);
    }

    /**
     * 修改活动
     */
    @Override
    public void update(BaseActivityDTO activityDTO) {
        log.info("更新ACTE023的活动");
        super.update(activityDTO);
    }

    /**
     * 删除活动
     */
    @Override
    public void delete(Long activityId) {
        log.info("删除ACTE023的活动");
        super.delete(activityId);
    }

    /**
     * 活动详情--pc
     */
    @Override
    public BaseActivityDTO detail(Long activityId) {
        log.info("查看ACTE023的活动详情");
        return super.detail(activityId);
    }

    /**
     * 参与活动
     */
    @Override
    public ActivityAwardVO takePartIn(FansActivityDTO fansActivityDTO) {
        log.info("进入{}", logPrefix);
        super.takePartIn(fansActivityDTO);
        Long fansId = fansActivityDTO.getFansId();
        Long activityId = fansActivityDTO.getActivityId();
        fansActivityDTO.setAgentNo(activityEntity.getAgentNo());

        log.info("用户基本信息：{}", JSONUtil.toJsonStr(fansEntity));

        JSONObject jsonObject = JSONObject.parseObject(fansActivityDTO.getExtJson());
        Integer scoreValue = jsonObject.getInteger("scoreValue");
        Integer overDays = jsonObject.getInteger("overDays");

        // 参数组装
        ScoreRecordTotalEntity scoreRecordTotalEntity = new ScoreRecordTotalEntity();
        scoreRecordTotalEntity.setFlag(0);
        scoreRecordTotalEntity.setInfoId(activityId);
        scoreRecordTotalEntity.setInfoType(Constants.SCORE.ACTIVITY);
        scoreRecordTotalEntity.setAppId(fansActivityDTO.getAppId());
        scoreRecordTotalEntity.setFansId(fansId);
        scoreRecordTotalEntity.setActivityId(activityId);
        scoreRecordTotalEntity.setScore(scoreValue);
        scoreRecordTotalEntity.setName(String.format("%s", activityEntity.getActivityName()));
        // 进行发积分处理
        // overType: 0 n天后过期 1 指定时间过期  2次季度首月月底失效
        scoreRecordTotalService.addScoreRecordTotal(fansActivityDTO.getAgentNo(), scoreRecordTotalEntity, overDays,0, null);
        log.info("{}用户:{}增加{}福分", logPrefix, fansId, jsonObject.getInteger("scoreValue"));

        return activityAwardService.toUserData(null);
    }

}
